/**
  ******************************************************************************
  * @author     Chris
  * @since      2024/6/25 13:24
  *
  * @file       pd_w25qxx.h
  * @brief      Header file of Xxx hardware driver.
  *
  * @note       This file contains the Xxx object declaration.
  *
  * @warning    None.
  ******************************************************************************
  * Change Logs:
  *   Date          Author       Notes
  *   2024/6/25     Chris        the first version
  *
  ******************************************************************************
  */


#ifndef JLC_SKY_W25QXX_H
#define JLC_SKY_W25QXX_H

#include "pd_spi.h"
#include "hd_storage.h"
#include "drivers.h"

typedef struct W25QxxDef W25Qxx;

struct W25QxxDef {
    Storage storage;

    SPI *spi;
    GPIO *ssel;

    void (*init)(W25Qxx *this);

    void (*getId)(W25Qxx *this, uint8_t *MID, uint16_t *DID);

    void (*read)(W25Qxx *this, uint32_t address, uint8_t *dat, uint32_t count);

    void (*eraseSector)(W25Qxx *this, uint32_t address);

    void (*write)(W25Qxx *this, uint32_t address, uint8_t *dat, uint16_t count);
};

extern const struct W25QxxClass {
    W25Qxx *(*build)(SPI *spi, GPIO *ssel);
} W25QxxBuilder;

#define W25QXX_SECTOR_SIZE                          4096
#define W25QXX_PAGE_SIZE                            256

#define W25QXX_WRITE_ENABLE                         0x06
#define W25QXX_WRITE_DISABLE                        0x04
#define W25QXX_READ_STATUS_REGISTER_1               0x05
#define W25QXX_READ_STATUS_REGISTER_2               0x35
#define W25QXX_WRITE_STATUS_REGISTER                0x01
#define W25QXX_PAGE_PROGRAM                         0x02
#define W25QXX_QUAD_PAGE_PROGRAM                    0x32
#define W25QXX_BLOCK_ERASE_64KB                     0xD8
#define W25QXX_BLOCK_ERASE_32KB                     0x52
#define W25QXX_SECTOR_ERASE_4KB                     0x20
#define W25QXX_CHIP_ERASE                           0xC7
#define W25QXX_ERASE_SUSPEND                        0x75
#define W25QXX_ERASE_RESUME                         0x7A
#define W25QXX_POWER_DOWN                           0xB9
#define W25QXX_HIGH_PERFORMANCE_MODE                0xA3
#define W25QXX_CONTINUOUS_READ_MODE_RESET           0xFF
#define W25QXX_RELEASE_POWER_DOWN_HPM_DEVICE_ID     0xAB
#define W25QXX_MANUFACTURER_DEVICE_ID               0x90
#define W25QXX_READ_UNIQUE_ID                       0x4B
#define W25QXX_JEDEC_ID                             0x9F
#define W25QXX_READ_DATA                            0x03
#define W25QXX_FAST_READ                            0x0B
#define W25QXX_FAST_READ_DUAL_OUTPUT                0x3B
#define W25QXX_FAST_READ_DUAL_IO                    0xBB
#define W25QXX_FAST_READ_QUAD_OUTPUT                0x6B
#define W25QXX_FAST_READ_QUAD_IO                    0xEB
#define W25QXX_OCTAL_WORD_READ_QUAD_IO              0xE3

#define W25QXX_DUMMY_BYTE                           0xFF

#define W25QXX_RESET_ENABLE                         0x66
#define W25QXX_RESET_MEMORY                         0x99

#endif //JLC_SKY_W25QXX_H
